﻿using System;

namespace ExFlash.Tags
{
    public enum CompressedFormat : byte
    {
        ColorMap8Bit = 3,
        RgbImage15Bit = 4,
        RgbImage24Bit = 5
    }

    public class DefineBitsLosslessTag : Tag
    {
        public ushort CharacterId
        {
            get;
            protected set;
        }

        public CompressedFormat BitmapFormat
        {
            get;
            protected set;
        }

        public ushort BitmapWidth
        {
            get;
            protected set;
        }

        public ushort BitmapHeight
        {
            get;
            protected set;
        }

        public Byte BitmapColorTableSize
        {
            get;
            protected set;
        }

        public Byte[] ZlibBitmapData
        {
            get;
            protected set;
        }

        protected DefineBitsLosslessTag(TagCodes code, int len, int offset)
            : base(code, len,offset)
        {

        }

        public DefineBitsLosslessTag(int len, int offset)
            : this(TagCodes.DefineBitsLossless, len, offset)
        {

        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            long startIndex = reader.Position;
            
            CharacterId = reader.ReadUInt16();

            BitmapFormat = (CompressedFormat)reader.ReadByte();

            BitmapWidth = reader.ReadUInt16();

            BitmapHeight = reader.ReadUInt16();

            if (BitmapFormat == CompressedFormat.ColorMap8Bit)
            {
                BitmapColorTableSize = reader.ReadByte();
            }

            int dataLength = TagLength - (int)(reader.Position - startIndex);
            ZlibBitmapData = new Byte[dataLength];
            reader.Read(ZlibBitmapData, 0, dataLength);
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }
    }
}
